<?php


namespace App\Http\Requests\ApiV2;

use App\Services\Business\Models\UserConquer;
use App\Services\Business\Models\WeightFragment;
use Illuminate\Support\Facades\Auth;

class WeightFragmentRequest extends BaseRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:120',

        ];
    }

    public function messages()
    {
        return [
            'title.required' => __('请输入标题'),
            'title.max' => __('课程标题的长度不能超过:size个字符', ['size' => 120]),

        ];
    }

    public function filldata()
    {
        $conquerId = (int)$this->input('conquer_id');
        $userId = Auth::id();

        $fragment = WeightFragment::query()
            ->where('user_id', $userId)
            ->where('conquer_id', $conquerId)
            ->when($this->method() == 'PUT', function ($query) use ($conquerId) {
                return $query->where('id', '<', $this->route('id'));
            })
            ->latest()->first();
        $record = UserConquer::query()->where('user_id', $userId)->where('conquer_id', $conquerId)->first();
        //跟上次比较的体重
        $lastDifferentWeight=  round($fragment ? $fragment['weight'] - $this->input('weight') : $record['weight'] - $this->input('weight'),2);
        //跟基础体重比较
        $totalDifferentWeight=  round($record['weight'] - $this->input('weight'),2);
        return [
            'user_id' => $userId,
            'title' => (string)$this->input('title'),
            'conquer_id' => $conquerId,
            'is_show' => (int)$this->input('is_show', 1),
            'images' => (array)$this->input('images', []),
            'video_ids' => (array)$this->input('video_ids', []),
            'weight' => $this->input('weight'),
            'last_different_weight' =>$lastDifferentWeight,
            'total_different_weight' => $totalDifferentWeight,

        ];
    }
}
